﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CongregacaoOs.Data;
using CongregacaoOs.Business;


namespace CongregacaoOs.WebForm.Cadastros
{
    public partial class Cargo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["DadosGerais"] == null) Response.Redirect("~/Cadastros/Account/Login.aspx");

            if (Session["TipoUsuario"].ToString() == "Local") Response.Redirect("~/Cadastros/Home.aspx");

            Page.Form.DefaultButton = btnBuscar.UniqueID;

            if (!IsPostBack)
                AtualizaGridView();
        }

        public void AtualizaGridView(string aNome = "", int aIdCargo = 0)
        {
            ccCargoBus bus = new ccCargoBus();
            ccCargo ent = new ccCargo();

            ent.nome = aNome;
            ent.idCargo = aIdCargo;

            gvCargo.DataSource = bus.CargoSelect(ent);
            gvCargo.DataBind();
        }

        protected void gvCargo_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            if (txtBuscarNome.Text != "")
                AtualizaGridView(txtBuscarNome.Text.Trim());
            else
            {
                txtBuscarNome.Text = "Digite o nome do país...";
                AtualizaGridView();
            }
        }

        protected void gvCargo_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "EDITAR" || e.CommandName == "EXCLUIR")
            {
                LinkButton linkID = new LinkButton();
                int index = int.Parse(e.CommandArgument.ToString());
                

                HiddenField hdIdCargo = (gvCargo.Rows[index].FindControl("hdIdCargo") as HiddenField);
                hdID.Value = hdIdCargo.Value;
                

                if (e.CommandName == "EDITAR")
                {
                    HiddenField hdCargo = (gvCargo.Rows[index].FindControl("hdCargo") as HiddenField);
                    hfIndexAccordion.Value = "1";
                    txtNome.Text = hdCargo.Value.ToString();
                }
                else if (e.CommandName == "EXCLUIR")
                {
                    ccCargoBus bus = new ccCargoBus();
                    bus.CargoDelete(Convert.ToInt32(hdIdCargo.Value));
                    
                }
                AtualizaGridView();
            }
        }

        protected void gvCargo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgEditar = (e.Row.FindControl("imgEditar") as ImageButton);
                imgEditar.CommandArgument = e.Row.RowIndex.ToString();

                ImageButton imgDelete = (e.Row.FindControl("imgDelete") as ImageButton);
                imgDelete.CommandArgument = e.Row.RowIndex.ToString();

                imgDelete.Attributes.Add("onclick", "javascript:return " +
                        "confirm('Confirmar exclusão do registro:" +
                        DataBinder.Eval(e.Row.DataItem, "nome") + "?')");
            }

        }

        protected void btnAdicionarNovo_Click(object sender, EventArgs e)
        {
            hdID.Value = null;
            hfIndexAccordion.Value = "1";
            txtNome.Text = string.Empty;
            Functions.LimparCampos(this);
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            try
            {
                ccCargoBus bus = new ccCargoBus();
                ccCargo ent = new ccCargo();

                ent.nome = txtNome.Text.Trim();

                if (hdID.Value != "") ent.idCargo = Convert.ToInt32(hdID.Value);

                bus.CargoSaveOrUpdate(ent);

                hfIndexAccordion.Value = "0";

                MessageWindow.MessageType = true;
                MessageWindow.Texto = "Cargo Salvo com Sucesso!!!";
                MessageWindow.ShowWindow();

                AtualizaGridView();
            }
            catch (Exception)
            {
                MessageWindow.MessageType = false;
                MessageWindow.Texto = "Houve um erro ao salvar o registro. Tente novamente ou contate o suporte!!!";
                MessageWindow.ShowWindow();
            }

        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            hfIndexAccordion.Value = "0";
            hdID.Value = "";
        }

        protected void gvCargo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            AtualizaGridView();
            gvCargo.PageIndex = e.NewPageIndex;
            gvCargo.DataBind();
        }
    }
}